﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eassets.Layered.Domain;
using Eassets.Service;

namespace Eassets.UI.GlobalReference
{
    public partial class EmployeeDetailsForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetAllEmployees();
                DisableComponents();

                buttonModify.Enabled = false;
            }
        }

        protected void DisableComponents()
        {
            dropDownListTitle.Enabled = false;
            textBoxNameInFull.Enabled = false;
            textBoxInitials.Enabled = false;
            textBoxUsedName.Enabled = false;
            textBoxEmail.Enabled = false;
            textBoxContactNo.Enabled = false;
            dropDownListGender.Enabled = false;
            textBoxDateOfBirth.Enabled = false;
            textBoxNICNo.Enabled = false;
            textBoxMaritalStatus.Enabled = false;
            dropDownListEmployeeType.Enabled = false;
            dropDownListUserGroup.Enabled = false;
            textBoxInitialPassword.Enabled = false;
            textBoxNewPassword.Enabled = false;
            textBoxConfirmPassword.Enabled = false;
        }

        protected void EnableComponents()
        {
            dropDownListTitle.Enabled = true;
            textBoxNameInFull.Enabled = true;
            textBoxInitials.Enabled = true;
            textBoxUsedName.Enabled = true;
            textBoxEmail.Enabled = true;
            textBoxContactNo.Enabled = true;
            dropDownListGender.Enabled = true;
            textBoxDateOfBirth.Enabled = true;
            textBoxNICNo.Enabled = true;
            textBoxMaritalStatus.Enabled = true;
            dropDownListEmployeeType.Enabled = true;
            dropDownListUserGroup.Enabled = true;
            textBoxInitialPassword.Enabled = true;
            textBoxNewPassword.Enabled = true;
            textBoxConfirmPassword.Enabled = true;
        }

        protected void ClearComponents()
        {
            textBoxNameInFull.Text = string.Empty;
            textBoxInitials.Text = string.Empty;
            textBoxUsedName.Text = string.Empty;
            textBoxEmail.Text = string.Empty;
            textBoxContactNo.Text = string.Empty;
            textBoxDateOfBirth.Text = string.Empty;
            textBoxNICNo.Text = string.Empty;
            textBoxMaritalStatus.Text = string.Empty;
            textBoxInitialPassword.Text = string.Empty;
            textBoxNewPassword.Text = string.Empty;
            textBoxConfirmPassword.Text = string.Empty;
        }

        private void GetAllEmployees()
        {
            EmployeeService employeeService = new EmployeeService();
            List<Employee> employees = new List<Employee>();
            employees = employeeService.GetAllEmployee().ToList();

            try
            {
                gridViewEmployeeDetails.DataSource = employees;
                gridViewEmployeeDetails.DataBind();
            }
            catch (Exception exception)
            { }
        }

        protected void buttonAdd_click(object sender, EventArgs e)
        {
            ClearComponents();
            EnableComponents();
            ViewState["ClickAdd"] = "Add";
            //GetAllEmployees();
        }

        protected void buttonSave_click(object sender, EventArgs e)
        {
            Employee employee = new Employee();
            EmployeeService employeeService = new EmployeeService();

            if ("Add" == Convert.ToString(ViewState["ClickAdd"]))
            {
                if (textBoxNewPassword.Text.Trim() == textBoxConfirmPassword.Text.Trim())
                {
                    employee.EmployeeName = textBoxNameInFull.Text;
                    employee.Title = dropDownListTitle.Text;
                    employee.Initials = textBoxInitials.Text;
                    employee.UserName = textBoxUsedName.Text;
                    employee.Email = textBoxEmail.Text;
                    employee.PhoneNumber = textBoxContactNo.Text;
                    employee.Gender = dropDownListGender.Text;
                    employee.DateOfBirth = Convert.ToDateTime(textBoxDateOfBirth.Text);
                    employee.NICNo = textBoxNICNo.Text;
                    employee.EmployeeType = dropDownListEmployeeType.Text;
                    employee.UserGroup = dropDownListUserGroup.Text;
                    employee.MaritalStatus = textBoxMaritalStatus.Text;
                    employee.InitialPassword = "";
                    employee.NewPassword = textBoxNewPassword.Text;
                    employee.ConfirmPassword = textBoxConfirmPassword.Text;

                    try
                    {
                        employeeService.AddEmployee(employee);
                        FlashMessage1.Message = "Saved Successfully";
                        FlashMessage1.CssClass = "okmessage";
                        FlashMessage1.Display();
                    }
                    catch (Exception ex)
                    {
                        FlashMessage1.Message = "Adding Failed";
                        FlashMessage1.CssClass = "errormessage";
                        FlashMessage1.Display();
                        //Response.Redirect("~/Error.aspx");
                    }
                    //Response.Redirect("EmployeeDetailsForm.aspx");
                }
                else
                { }
            }

            else if ("Modify" == Convert.ToString(ViewState["ClickModify"]))
            {
                employee.EmployeeName = textBoxNameInFull.Text;
                employee.Title = dropDownListTitle.Text;
                employee.Initials = textBoxInitials.Text;
                employee.UserName = textBoxUsedName.Text;
                employee.Email = textBoxEmail.Text;
                employee.PhoneNumber = textBoxContactNo.Text;
                employee.Gender = dropDownListGender.Text;
                employee.DateOfBirth = Convert.ToDateTime(textBoxDateOfBirth.Text);
                employee.NICNo = textBoxNICNo.Text;
                employee.EmployeeType = dropDownListEmployeeType.Text;
                employee.UserGroup = dropDownListUserGroup.Text;
                employee.MaritalStatus = textBoxMaritalStatus.Text;
                employee.InitialPassword = "";
                employee.NewPassword = textBoxNewPassword.Text;
                employee.ConfirmPassword = textBoxConfirmPassword.Text;

                try
                {
                    employeeService.ModifyEmployee(employee);
                    FlashMessage1.Message = "Saved Successfully";
                    FlashMessage1.CssClass = "okmessage";
                    FlashMessage1.Display();
                }
                catch (Exception ex)
                {
                    FlashMessage1.Message = "Modification Failed";
                    FlashMessage1.CssClass = "errormessage";
                    FlashMessage1.Display();
                    //Response.Redirect("~/Error.aspx");
                }
                //Response.Redirect("EmployeeDetailsForm.aspx");
            }
            //GetAllEmployees();
        }

        //private Boolean CheckMandatery(String function)
        //{

        //    DateTime testJondate = (Convert.ToDateTime(DateTime.Today.ToString(Session[Global.KeyDateFormat].ToString()))).AddYears(-18);

        //    if (function == "A")
        //    {
        //        if (Convert.ToDateTime(textBoxDateOfBirth.Text.Trim()) > testJondate)
        //        {
        //            FlashMessage1.Display("[Joined Date] cannot be a future date", "errormessage");
        //            textBoxDateOfBirth.Focus();
        //            return false;
        //        }
        //        else
        //        {
        //            return true;
        //        }

        //    }
        //}

        protected void gridViewEmployeeDetails_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = gridViewEmployeeDetails.SelectedRow;
            if (row != null)
            {
                textBoxNameInFull.Text = row.Cells[2].Text.Trim();
                dropDownListEmployeeType.SelectedItem.Text = row.Cells[3].Text;
                textBoxInitials.Text = row.Cells[5].Text;
                textBoxUsedName.Text = row.Cells[6].Text;
                textBoxEmail.Text = row.Cells[7].Text;
                textBoxContactNo.Text = row.Cells[8].Text;
                dropDownListGender.SelectedItem.Text = row.Cells[9].Text;
                textBoxDateOfBirth.Text = row.Cells[10].Text;
                textBoxNICNo.Text = row.Cells[11].Text;
                textBoxMaritalStatus.Text = row.Cells[13].Text;
            }
        }

        protected void gridViewEmployeeDetails_OnRowCreated(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 1)
            {
                if (e.Row.Cells.Count > 1)
                {
                    e.Row.Cells[5].Visible = false;
                    e.Row.Cells[4].Visible = false;
                    e.Row.Cells[6].Visible = false;
                    e.Row.Cells[7].Visible = false;
                    e.Row.Cells[8].Visible = false;
                    e.Row.Cells[9].Visible = false;
                    e.Row.Cells[10].Visible = false;
                    e.Row.Cells[11].Visible = false;
                    e.Row.Cells[13].Visible = false;
                }
            }
        }

        protected void buttonModify_Click(object sender, EventArgs e)
        {
            GridViewRow gridViewRow = gridViewEmployeeDetails.SelectedRow;
            if (gridViewRow != null)
            {
                ViewState["ClickModify"] = "Modify";
                EnableComponents();
                //GetAllEmployees();
            }
            else
            {
                FlashMessage1.Message = "Please select a row on Grid";
                FlashMessage1.CssClass = "errormessage";
                FlashMessage1.Display();
            }
        }

        private void ValidateComponents()
        { }

    }
}